package com.wxzz.elearing.comment.api.admin;


import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import com.wxzz.elearing.comment.dto.SkillCenterCategoryDto;
import com.wxzz.elearing.comment.entity.SkillCenterCategory;
import com.wxzz.elearing.comment.service.ISkillCenterCategoryService;
import com.wxzz.elearning.common.exception.ServiceException;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 技能沉淀/中心分类 前端控制器(backend)
 * </p>
 *
 * @author 
 * @since 2021-05-17
 */
@RestController
@RequestMapping("/skill-center-category")
@Api(tags = "技能分类接口(企业)")
public class SkillCenterCategoryController {

    @Autowired
    private ISkillCenterCategoryService skillCenterCategoryService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "新增技能分类(企业)", notes = "新增或修改技能分类(企业)", httpMethod = "POST", tags = "企业")
    @RequestMapping(value = "saveCategory", method = RequestMethod.POST)
    public ReturnVO saveCategory(@RequestHeader String token, @RequestBody SkillCenterCategory skillCenterCategory) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        if (jsonObject==null){
            throw new ServiceException("请先登录");
        }
        Long enterpriseId = jsonObject.getLong("enterpriseId");
        if (skillCenterCategory.getId() != null && skillCenterCategory.getEnterpriseId() == null){
            throw new ServiceException("提交信息有误，请获取正确的数据");
        }
        skillCenterCategory.setEnterpriseId(enterpriseId);
        return skillCenterCategoryService.saveCategory(skillCenterCategory);
    }

    @ApiOperation(value = "删除技能分类(企业)", notes = "删除技能分类(企业)", httpMethod = "POST", tags = "企业")
    @RequestMapping(value = "deleteCategory/{id}", method = RequestMethod.POST)
    public ReturnVO deleteCategory(@RequestHeader String token, @PathVariable Long id) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        if (jsonObject==null){
            throw new ServiceException("请先登录");
        }
        Long enterpriseId = jsonObject.getLong("enterpriseId");
        return skillCenterCategoryService.deleteCategory(id, enterpriseId);
    }

    @ApiOperation(value = "技能分类列表(企业)", notes = "技能分类列表(企业)", httpMethod = "POST", tags = "企业")
    @RequestMapping(value = "listCategory/{type}", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<SkillCenterCategory>>> listCategory(@RequestHeader String token, @RequestBody SkillCenterCategoryDto skillCenterCategoryDto, @PathVariable(value = "type", required = false) Integer type) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        if (jsonObject==null){
            throw new ServiceException("请先登录");
        }
        Long enterpriseId = jsonObject.getLong("enterpriseId");
        if (type==null) type = 1;
        return skillCenterCategoryService.listCategory(skillCenterCategoryDto, enterpriseId, type);
    }

}
